package cn.xej.config;

import cn.xej.callback.RabbitConfirmCallback;
import cn.xej.callback.RabbitReturnCallback;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author alicat.xu
 * @Date 2021/10/20 3:10 下午
 */
@Configuration
public class RabbitmqConfig {

    @Autowired
    private RabbitReturnCallback rabbitReturnCallback;

    @Autowired
    private RabbitConfirmCallback rabbitConfirmCallback;

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);

        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(rabbitConfirmCallback);
        rabbitTemplate.setReturnsCallback(rabbitReturnCallback);

//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean b, String s) {
//                System.out.println("RabbitConfirmCallback.....数据.. "+correlationData);
//                System.out.println("RabbitConfirmCallback.....确认情况.. "+b);
//                System.out.println("RabbitConfirmCallback.....原因.. "+s);
//            }
//        });
//
//        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
//            @Override
//            public void returnedMessage(ReturnedMessage returnedMessage) {
//                System.out.println("RabbitReturnCallback..... 消息.. "+returnedMessage);
//            }
//        });


        return rabbitTemplate;
    }

}

